package com.xykj.enterprise.wechat.ext.api.externalcontact;

import com.xykj.enterprise.wechat.bean.ext.externalcontact.AddContactWayVo;
import com.xykj.enterprise.wechat.ext.service.customer.ExternalContactService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "配置客户联系「联系我」方式", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "type", type = Type.Integer, description = "联系方式类型,1-单人, 2-多人", required = true),
        @Parameter(name = "user", type = Type.String, description = "使用该联系方式的用户userID列表，在type为1时为必填，且只能有一个", required = false),
        @Parameter(name = "party", type = Type.String, description = "使用该联系方式的部门id列表，只在type为2时有效", required = false),
        @Parameter(name = "skip_verify", type = Type.Boolean, description = "外部客户添加时是否无需验证，默认为true", required = false),
        @Parameter(name = "remark", type = Type.String, description = "联系方式的备注信息，用于助记，不超过30个字符", required = false),
})
@Component
public class AddContactWayQrcode implements Action {

    @Autowired
    private ExternalContactService externalContactService;

    @Override
    public Response execute(Request request) throws Exception {
        Integer type = request.getInteger("type");
        String user = request.getString("user");
        String party = request.getString("party");
        Boolean skip_verify = request.getBoolean("skip_verify");
        String remark = request.getString("remark");
        HashMap<String, Object> params = new HashMap<String, Object>();
        if (type.intValue() == 1) {
            if (user == null) {
                return Response.fail("参数错误！");
            }
            String[] userSplit = user.split(",");
            params.put("user", userSplit);
        } else {
            if (user == null && party == null) {
                return Response.fail("参数错误！");
            }
            String[] userSplit = user.split(",");
            params.put("user", userSplit);
            String[] partySplit = party.split(",");
            params.put("party", partySplit);
        }
        params.put("scene", 2);
        params.put("type", type);
        if (skip_verify != null) {
            params.put("skip_verify", skip_verify);
        }
        if (remark != null) {
            params.put("remark", remark);
        }
        AddContactWayVo rsp = externalContactService.addContactWayQrcode(
                request.getString("access_token"), params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }
}
